package com.xsomnus.network_programming.udp.multicast;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.logging.Logger;

/**
 * @author 叶小夏
 * @since 2019-08-16 15:28
 */
public class MReceive {
    public static void main(String[] args) {
        new MReceive().run(2011);
    }


    private void run(int port) {
        String host = "224.116.8.0";
        try (MulticastSocket socket = new MulticastSocket(port);) {
            InetAddress mcastaddr = InetAddress.getByName(host);
            socket.joinGroup(mcastaddr);
            byte b[] = new byte[512];
            DatagramPacket pack = new DatagramPacket(b, b.length, mcastaddr, port);
            while (true) {
                socket.receive(pack);
                String msg = new String(pack.getData(), 0, b.length);
                Logger.getGlobal().info(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
